package com.ys.controller.web.purchase;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ys.service.purchase.TcgPurchaseAppointOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.purchase.TcgPurchaseAppointOrder;
import com.ys.entity.purchase.TcgPurchaseAppointOrderItem;
import com.ys.service.flow.YspTaskService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.Flow;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/purchase/tcgPurchaseAppointOrder")
public class WebTcgPurchaseAppointOrderController {
    private static final Logger logger = LoggerFactory.getLogger(WebTcgPurchaseAppointOrderController.class);

    @Autowired
    private CommonApiService<TcgPurchaseAppointOrder> commonApiService;

    @Autowired
    private YspTaskService yspTaskService;

    //菜单id
    private Long menuId = MenuId.TCG_PurchaseAppointOrder;

    @Autowired
    private TcgPurchaseAppointOrderService tcgPurchaseAppointOrderService;
    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model) {
        List<Map<String, Object>> sonlist = new ArrayList<>();

        // 子列表配置信息
        SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId, sonlist);
        try {
            String bid = commonApiService.getCommonNextBid(menuId);

            TcgPurchaseAppointOrder entity = new TcgPurchaseAppointOrder()
                    .setBid(bid)
                    .setCompanyVid("C104")
                    .setCreateTime(new Date());
            Map<String, Object> extraParam = new HashMap<String, Object>();
            extraParam.put("companyName", "福州福沃洋国际商贸有限公司");
            extraParam.put("companyVid", "C104");

            return commonApiService.toAdd(model, entity, menuId, extraParam, subGridConfig);

        } catch (Exception e) {
            e.printStackTrace();
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, String bid) {
        try {
            if (bid == null) {
                return W.error(model, "单号不能为空");
            }

            Map<String, Object> entity = tcgPurchaseAppointOrderService.getExtraParam(bid);
            if (entity == null) {
                return W.error(model, "表单不存在!");
            }
            List<Map<String, Object>> sonlist = tcgPurchaseAppointOrderService.getItemList(bid);

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId, sonlist);

            //子表按钮
            Integer state = yspTaskService.flowState(bid);
            if (state == Flow.State.FINISH.value) {
                subGridConfig.setButtonArr(new ArrayList<>());
            } else if (state == Flow.State.PROCESS.value) {
                subGridConfig.setButtonArr(new ArrayList<>());
            }
            return commonApiService.toEdit(model, Long.parseLong(entity.get("id").toString()), new TcgPurchaseAppointOrder(), menuId, entity, subGridConfig);
        } catch (Exception e) {
            logger.error("编辑页面出错：", e.getMessage(), e);
            return W.error(model, e.getMessage());
        }
    }
}
